var Data={
    ajaxLoadingImg: 'img.ajax-loader',

    filterChange: function (field,ajax){
        var input=$(field);
        var name=input.attr('name').replace('filter[','').replace(']','');
        var value=input.val();
        var link=Data.filterChangeUrl(input.attr('data-id'),name,value);

        if(ajax) return Data.ajaxSend(link,'function=filter',true,Data.contentDiv());
        else location.href=link;
    },

    filterKeyup: function(field){
        var input=$(field);
        var name=input.attr('name').replace('filter[','').replace(']','');
        var value=input.val();
        var link=Data.filterChangeUrl(input.attr('data-id'),name,value);

        if(value.length>0){
            var data="filterName="+name+"&filterValue="+value;
            var hintBlock=$("#hint-".input.attr('id'));
            Data.ajaxSend(link,data,true,hintBlock);
        }
    },

    selectElementsPerPage: function(option,ajax){
        var selectedNum=$(option).val();
        var link=$("#elements-per-page-"+selectedNum).attr('data-id');
        if(ajax){
            return Data.ajaxSend(link,'function=elementsCount',true,Data.contentDiv());
        }else location.href=link;
    },

    pagination: function(button,ajax){
        if(ajax){
            return Data.ajaxSend($(button).attr('href'),'function=pagination',true,Data.contentDiv());
        }else location.href=$(button).attr('href');
    },

    dataSort: function(link,ajax){
        var url=$(link).attr('href');
        var reg='';
        url=url.replace(new RegExp(reg),'');
        if(ajax){
            return Data.ajaxSend(url,'function=dataSort',true,Data.contentDiv());
        }else location.href=url;
    },

    ajaxSend: function(link,data,loading,ajaxElement){
        $.ajax({
            url:   link,
            type:  'post',
            data:  data,
            dataType: 'json',
            beforeSend: function(){
                if(loading) Data.ajaxLoadingShow();
            },
            complete: function(){
                if(loading) Data.ajaxLoadingHide();
            },
            error: function(request,error)
            {
                $('div.error').html('Не удалось выполнить запрос: '+error);
            },
            success: function(json){
                if(json.content) ajaxElement.html(json.content);
                if(json.error) $('div.error').html(json.error);
            }
        });
        return false;
    },

    ajaxLoadingShow: function(){
        $(Data.ajaxLoadingImg).css('display','block');
    },

    ajaxLoadingHide: function(){
        $(Data.ajaxLoadingImg).css('display','none');
    },

    contentDiv: function(){
        return $('div.content').parent('div');
    },

    filterChangeUrl: function(link,name,value){
        var reg="\/"+name+"\/like(.*?)\/";
        if(link.match(new RegExp(reg))==null){
            reg="\/"+name+"\/like(.*)";
            link=link.replace(new RegExp(reg),'');
        }else{
            link=link.replace(new RegExp(reg),'/');
        }

        if(value.length>0) link+='/'+name+'/like '+value;

        reg='limit\/\\d{1,9}\,\\s';
        if(link.match(new RegExp(reg))==null) reg='limit\/\\d{1,9}\\s';
        link=link.replace(new RegExp(reg),'limit/');

        return link;
    }
}

